/*******************************************************************************
  * Copyright(c) 2023 Levetop Semiconductor Co.,Led. All rights reserved.
  * @file     analogClock.c
  * @author   UartTFT Application Team
  * @version  V0.0.1
  * @date     2023-01-04
  * @brief    This file contains the functions prototypes for the UartTFT
  *           Application display firmware.
 ********************************************************************************/

#include "analogClock.h"
#include "levetop.h"
#include "bsp.h"
#include "rtc.h"
#include "keyBoard.h"

uint8_t Clock_num = 0;
uint8_t Clock_flag = 0;
Clock_Info gClock_Info[CLOCK_SIZE];
uint32_t Lay_temp = 0;

void show_Clock(void)
{
	if (Clock_num > 0 && Clock_flag == 1)
	{
		Clock_flag = 0;
		for (uint8_t i = 0; i < Clock_num; i++)
		{
			Display_Clock(i, 1);
		}
	}
}

/***********************************************************************************
* Function Name : Display_Clock
* Description   : Show analog clock.
* Input         : - num: analog clock number
				  - status: analog clock status
* Output        : None
* Return        : None
************************************************************************************/
uint8_t Display_Clock(uint8_t num, uint8_t status)
{
	uint8_t hour, min, sec;
	uint8_t hour_temp;

	hour = calendar.hour;
	min = calendar.min;
	sec = calendar.sec;
	hour = hour % 12;

	if (gClock_Info[num].Image_BG < addr_index[11]) // ICON is available
	{
		if (gClock_Info[num].len_pic != 0 && gClock_Info[num].flag_pic != 0xFF) // ICON is not empty
		{
			// In the keyboard input interface
			if (DioKb_flag == 1)
			{
				if (!(gClock_Info[num].x_pic > gDio_Info[Dio_num].Xe || (gClock_Info[num].x_pic + gClock_Info[num].w_pic) < gDio_Info[Dio_num].Xs || gClock_Info[num].y_pic > gDio_Info[Dio_num].Ye || (gClock_Info[num].y_pic + gClock_Info[num].h_pic) < gDio_Info[Dio_num].Ys))
				{
					return 0;
				}
			}
			else if (AioKb_flag == 1)
			{
				if (!(gClock_Info[num].x_pic > gAio_Info[Aio_num].Xe || (gClock_Info[num].x_pic + gClock_Info[num].w_pic) < gAio_Info[Aio_num].Xs || gClock_Info[num].y_pic > gAio_Info[Aio_num].Ye || (gClock_Info[num].y_pic + gClock_Info[num].h_pic) < gAio_Info[Aio_num].Ys))
				{
					return 0;
				}
			}
			else if (GBKioKb_flag == 1)
			{
				if (!(gClock_Info[num].x_pic > gGBKio_Info[GBKio_num].Xe || (gClock_Info[num].x_pic + gClock_Info[num].w_pic) < gGBKio_Info[GBKio_num].Xs || gClock_Info[num].y_pic > gGBKio_Info[GBKio_num].Ye || (gClock_Info[num].y_pic + gClock_Info[num].h_pic) < gGBKio_Info[GBKio_num].Ys))
				{
					return 0;
				}
			}

			// full-screen background
			if (gClock_Info[num].flag_pic == 1 || gClock_Info[num].flag_pic == 2)
			{
				LT_getBackGround(gClock_Info[num].x_pic, gClock_Info[num].y_pic,gClock_Info[num].w_pic, gClock_Info[num].h_pic, m_PngData, PIC_ON_BaseImg);
			}

			// clock background
			LT268_pic_to_buffer(0, 0, gClock_Info[num].w_pic, m_PngData,
								0, 0, gClock_Info[num].w_pic, gClock_Info[num].addr_pic,
								gClock_Info[num].w_pic, gClock_Info[num].h_pic, gClock_Info[num].flag_pic);

			// Pointer of clock
			DrawClockPointer(gClock_Info[num].x, gClock_Info[num].y, hour * 30 + min * 30 / 60, gClock_Info[num].L_hour, gClock_Info[num].S_hour, gClock_Info[num].Color_hour, gClock_Info[num].W_hour,
							 gClock_Info[num].x, gClock_Info[num].y, min * 6 + sec * 6 / 60, gClock_Info[num].L_min, gClock_Info[num].S_min, gClock_Info[num].Color_min, gClock_Info[num].W_min, // min*6+sec*6/60
							 gClock_Info[num].x, gClock_Info[num].y, sec * 6, gClock_Info[num].L_sec, gClock_Info[num].S_sec, gClock_Info[num].Color_sec, gClock_Info[num].W_sec,
							 gClock_Info[num].x_pic, gClock_Info[num].y_pic, gClock_Info[num].w_pic, gClock_Info[num].h_pic);

			// center ICON
			if (gClock_Info[num].len_cen != 0 && gClock_Info[num].flag_cen != 0xFF)
			{
				pic_flag = gClock_Info[num].flag_cen;

				LT268_pic_to_buffer(gClock_Info[num].x_cen - gClock_Info[num].x_pic, gClock_Info[num].y_cen - gClock_Info[num].y_pic, gClock_Info[num].w_pic, m_PngData,
									0, 0, gClock_Info[num].w_cen, gClock_Info[num].addr_cen,
									gClock_Info[num].w_cen, gClock_Info[num].h_cen, gClock_Info[num].flag_cen);
			}

			LT268_buffer_to_TFT(0, 0, gClock_Info[num].w_pic, m_PngData,
								gClock_Info[num].x_pic, gClock_Info[num].y_pic, gClock_Info[num].w_pic, gClock_Info[num].h_pic);
		}
	}
}

/***********************************************************************************
* Function Name	: Clock_display
* Description   : Get analog clock info.
* Input         : - type: analog clock info sources
				  - buff: analog clock info
* Output        : None
* Return        : None
************************************************************************************/
void Clock_display(uint8_t type, uint8_t *buff)
{
	uint8_t buff1[12] = {0};
	uint8_t L_hour, S_hour, W_hour, L_min, S_min, W_min, L_sec, S_sec, W_sec;
	uint16_t pointer;
	uint16_t x, y, image, image_cen;
	uint32_t Color_hour = 0, Color_min = 0, Color_sec = 0;

	if (type == 0) // Instruction information source is configuration file
	{
		x = buff[5] + (buff[6] << 8);
		y = buff[7] + (buff[8] << 8);
		image = buff[9] + (buff[10] << 8);
		L_hour = buff[11];
		S_hour = buff[12];
		W_hour = buff[13];
		Color_hour = buff[14] + (buff[15] << 8) + (buff[16] << 16);
		L_min = buff[17];
		S_min = buff[18];
		W_min = buff[19];
		Color_min = buff[20] + (buff[21] << 8) + (buff[22] << 16);
		L_sec = buff[23];
		S_sec = buff[24];
		W_sec = buff[25];
		Color_sec = buff[26] + (buff[27] << 8) + (buff[28] << 16);
		image_cen = buff[29] + (buff[30] << 8);
	}
	else if (type == 1) // Instruction information ource is variable space
	{
		pointer = (buff[2] + (buff[3] << 8)) * 2;
		x = var[pointer + 1] + (var[pointer + 2] << 8);
		y = var[pointer + 3] + (var[pointer + 4] << 8);
		image = var[pointer + 5] + (var[pointer + 6] << 8);
		L_hour = var[pointer + 7];
		S_hour = var[pointer + 8];
		W_hour = var[pointer + 9];
		Color_hour = var[pointer + 10] + (var[pointer + 11] << 8) + (var[pointer + 12] << 16);
		L_min = var[pointer + 13];
		S_min = var[pointer + 14];
		W_min = var[pointer + 15];
		Color_min = var[pointer + 16] + (var[pointer + 17] << 8) + (var[pointer + 18] << 16);
		L_sec = var[pointer + 19];
		S_sec = var[pointer + 20];
		W_sec = var[pointer + 21];
		Color_sec = var[pointer + 22] + (var[pointer + 23] << 8) + (var[pointer + 24] << 16);
		image_cen = var[pointer + 25] + (var[pointer + 26] << 8);
	}

	if (Pop_up_flag == 1) // Popup offset
	{
		x += Pop_up_x;
		y += Pop_up_y;
	}
	gClock_Info[Clock_num].x_pic = x;
	gClock_Info[Clock_num].y_pic = y;
	gClock_Info[Clock_num].Image_BG = image;
	gClock_Info[Clock_num].L_hour = L_hour;
	gClock_Info[Clock_num].S_hour = S_hour;
	gClock_Info[Clock_num].W_hour = W_hour;
	gClock_Info[Clock_num].Color_hour = Color_hour;
	gClock_Info[Clock_num].L_min = L_min;
	gClock_Info[Clock_num].S_min = S_min;
	gClock_Info[Clock_num].W_min = W_min;
	gClock_Info[Clock_num].Color_min = Color_min;
	gClock_Info[Clock_num].L_sec = L_sec;
	gClock_Info[Clock_num].S_sec = S_sec;
	gClock_Info[Clock_num].W_sec = W_sec;
	gClock_Info[Clock_num].Color_sec = Color_sec;

	if (image < addr_index[11]) // ICON is not empty
	{
		LT_ReadFlash_UI(buff1, addr_index[10] + 12 * image + multi_language * 12 * addr_index[11], 12);

		gClock_Info[Clock_num].addr_pic = buff1[0] + (buff1[1] << 8) + (buff1[2] << 16) + (buff1[3] << 24);
		gClock_Info[Clock_num].w_pic = buff1[4] + (buff1[5] << 8);
		gClock_Info[Clock_num].h_pic = buff1[6] + (buff1[7] << 8);
		gClock_Info[Clock_num].len_pic = buff1[8] + (buff1[9] << 8) + (buff1[10] << 16);
		gClock_Info[Clock_num].flag_pic = buff1[11];
		gClock_Info[Clock_num].x = x + gClock_Info[Clock_num].w_pic / 2;
		gClock_Info[Clock_num].y = y + gClock_Info[Clock_num].h_pic / 2;

		if (image_cen < addr_index[11]) // ICON is not empty
		{
			LT_ReadFlash_UI(buff1, addr_index[10] + 12 * image_cen + multi_language * 12 * addr_index[11], 12);

			gClock_Info[Clock_num].addr_cen = buff1[0] + (buff1[1] << 8) + (buff1[2] << 16) + (buff1[3] << 24);
			gClock_Info[Clock_num].w_cen = buff1[4] + (buff1[5] << 8);
			gClock_Info[Clock_num].h_cen = buff1[6] + (buff1[7] << 8);
			gClock_Info[Clock_num].len_cen = buff1[8] + (buff1[9] << 8) + (buff1[10] << 16);
			gClock_Info[Clock_num].flag_cen = buff1[11];
			gClock_Info[Clock_num].x_cen = gClock_Info[Clock_num].x - gClock_Info[Clock_num].w_cen / 2;
			gClock_Info[Clock_num].y_cen = gClock_Info[Clock_num].y - gClock_Info[Clock_num].h_cen / 2;
		}
	}
	Display_Clock(Clock_num, 0);
	Clock_num++;
}
